package cn.hengzhu.main_manager.utils;


import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

public class IpAdrrUtil {
	
	public static String getClientIpAddr(HttpServletRequest request){
		if (request == null) {
			return "";
		}
		String ipString = request.getHeader("X-Real-IP");
		
		if (StringUtils.isEmpty(ipString) || "unknown".equalsIgnoreCase(ipString)) {
			ipString = request.getHeader("Proxy-Client-IP");
		}
		if (StringUtils.isEmpty(ipString) || "unknown".equalsIgnoreCase(ipString)) {
			ipString = request.getHeader("WL-Proxy-Client-IP");
		}
		if (StringUtils.isEmpty(ipString) || "unknown".equalsIgnoreCase(ipString)) {  
			ipString = request.getHeader("HTTP_CLIENT_IP");  
        }  
		if (StringUtils.isEmpty(ipString) || "unknown".equalsIgnoreCase(ipString)) {  
			ipString = request.getHeader("HTTP_X_FORWARDED_FOR");  
        }  
		if (StringUtils.isEmpty(ipString) || "unknown".equalsIgnoreCase(ipString)) {  
			ipString = request.getHeader("X-Forwarded-For");
        }  
		if (StringUtils.isEmpty(ipString) || "unknown".equalsIgnoreCase(ipString)) {
			ipString = request.getRemoteAddr();
		}
		final String[] arr = ipString.split(",");
		for (final String str : arr) {
			if (!"unknown".equalsIgnoreCase(str)) {
				ipString = str;
				break;
			}
		}
		return ipString;
	}

}
